<?php
class App_Module_Admin_Component_Articles_Ctrl_Http extends Mozg_Controller_Instance
{
	protected $_newsCategoryPageType = 2;

    protected $_page = 1;
    protected $_blog;
    protected $_entry;

    protected function GetBlogFromQueryId()
    {
    	if ($this->_blog !== null) return $this->_blog;

    	$this->_blog = Mozg_Mapper::Get('Blogs')->GetEntity();
    	$this->_blog->SetTitle(Mozg_Lang::_('ARTICLES'));

		return $this->_blog;
    }

    protected function GetEntryFromQueryId($blogId)
    {
    	if ($this->_entry !== null) return $this->_entry;

		$id = (int)Mozg::Request('http')->get('id');
		if ($id)
		{
			$this->_entry = Mozg_Mapper::Get('Articles')->GetOne(array('id' => $id, 'blogId' => $blogId));
			return $this->_entry;
		}
		else
		{
			$this->_entry = false;
		}

		return $this->_entry;
    }

    protected function GetQueryPage($setPage = null)
    {
    	if (is_null($page) && Mozg::Request('http')->hasQuery('page'))
    	{
    		$setPage = (int)Mozg::Request('http')->getQuery('page');
    	}

    	if (!is_null($setPage))
    	{
			$this->_page = (int)$setPage;
			if ($this->_page != Mozg::Request('http')->getCookie('page'))
			{
				Mozg::Request('http')->SetCookie('page', $this->_page, 0, Mozg::Router('http')->Create('module=admin&ctrl=' .$this->GetName()));
			}
    	}
    	else
    	{
    		$this->_page = (int)Mozg::Request('http')->getCookie('page');
    	}

    	return $this->_page;
    }

    protected function GetFilter($blogId)
    {
		$filter = Mozg_Mapper::Get('Articles')->GetFilter();
		$filter->SetBlogId($blogId);

		$session = Mozg_Mapper::Get('Session')->GetEntity('admin', 'articlefilter' .$this->GetName());

		if (Mozg::Request('http')->hasQuery('resetfilter'))
		{
			Mozg_Mapper::Get('Session')->Delete($session);
			return $filter;
		}
		else if (Mozg::Request('http')->hasQuery('articlefilter'))
		{
	   		$this->GetTpl('Filterform')->SetPostData($filter, (array)Mozg::Request('http')->getQuery('articlefilter'));

	   		$session->ImportData($filter->ExportData());
	   		Mozg_Mapper::Get('Session')->Save($session);

	   		$this->GetQueryPage(1);
		}
		else
		{
			$filter->AddData($session->ExportData());
		}

    	return $filter;
    }

    protected function GetMainThemeId($blogId)
    {
    	$session = Mozg_Mapper::Get('Session')->GetEntity('admin', 'articletheme');

    	if (Mozg::Request('http')->has('main_theme_id'))
    	{
    		$session->SetId((int)Mozg::Request('http')->get('main_theme_id'));
    		Mozg_Mapper::Get('Session')->Save($session);
    	}

        if ($session->GetId())
		{
			$themeId = $session->GetId();
		}
		else
		{
			$themeId = 0;
		}

    	return $themeId;
    }

    protected function OutArticles($blog)
    {
    	$this->GetQueryPage();

        $filter = $this->GetFilter($blog->GetId());

        $t = $this->GetTpl('List');
        $content = $t->GetList($this->GetName(), $blog, $filter, $this->_page, $this->GetMainThemeId($blog->GetId()));

        Mozg::Response('http')->SetBody($content);
        Mozg::SubAction()->Set('Left Sidebar', array($this->GetTpl('Filterform'), 'GetForm'), array($this->GetName(), $blog, $filter));
    }

    protected function AddArticle($blog, $entry = null)
    {
		$saveProcess = false;
        if ($entry)
        {
        	$saveProcess = true;
        }

        if (!$entry)
        {
        	$entry = $this->GetEntryFromQueryId($blog->GetId());
	   		if (!$entry)
	   		{
	   			$entry = Mozg_Mapper::Get('Articles')->GetEntity();
	   			$entry->SetBlogId($blog->GetId());
	   		}
        }

        $t = $this->GetTpl('Editform');
        Mozg::Response('http')->SetBody($t->GetForm($this->GetName(), $blog, $entry));

        $baseUrlParams = array(
        						'module' => 'admin',
        						'ctrl' => $this->GetName(),
        						'action' => 'mediaadd',
        						'blog' => $blog->GetId(),
        						'id' => $entry->GetId()
        					);

        Mozg::SubAction()->Set('Left Sidebar', array($this->GetCtrl('media'), 'mediablock'), array('baseUrl' => $baseUrlParams, 'params' => array('contentButton' => true)));
    }

    protected function MediaForArticle($blog)
    {
    	$entry = $this->GetEntryFromQueryId($blog->GetId());

        $baseUrlParams = array(
        						'module' => 'admin',
        						'ctrl' => $this->GetName(),
        						'action' => 'mediaadd',
        						'blog' => $blog->GetId(),
        						'id' => $entry ? $entry->GetId() : 0,
        					);

		$data = $this->GetCtrl('media')->OutMediaFilesBlock($baseUrlParams, array('contentButton' => true));
        Mozg::Response('http')->SetBody($data);
    }

    protected function SaveArticle($blog)
    {
       	$entry = $this->GetEntryFromQueryId($blog->GetId());
       	if (!$entry)
       	{
       		$entry = Mozg_Mapper::Get('Articles')->GetEntity();
       		$entry->SetBlogId($blog->GetId());
       	}

   		$this->GetTpl('Editform')->AddPostData($entry, (array)Mozg::Request('http')->getPost('article'));

		if (Mozg_Mapper::Get('Articles')->Save($entry))
        {
        	Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_SAVED'));

        	Mozg::Router('http')->Redirect('module=admin&ctrl=' .$this->GetName(). '&action=list&blog=' .$blog->GetId());
        }
        else
        {
        	if (!$entry->Validator()->GetState())
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
        	}
        	else
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_NOT_SAVED'), MESSAGE_ERROR);
        	}

        	$this->AddArticle($blog, $entry);
        	return;
        }
    }

    protected function DelArticle($blog)
    {
       	$entry = $this->GetEntryFromQueryId($blog->GetId());
   		if (!$entry)
   		{
   			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('ARTICLE_NOT_FOUND'), MESSAGE_ERROR);
        	Mozg::Router('http')->Redirect('module=admin&ctrl=' .$this->GetName(). '&action=list&blog=' .$blog->GetId());
   		}

   		if (Mozg_Mapper::Get('Articles')->Delete(array('id' => $entry->GetId(), 'blogId' => $blog->GetId())))
   		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('ARTCILE_DELETED'));
   		}
   		else
   		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('UNABLE_TO_DELETE_ARTICLE'), MESSAGE_ERROR);
   		}

       	Mozg::Router('http')->Redirect('module=admin&ctrl=' .$this->GetName(). '&action=list&blog=' .$blog->GetId());
    }

    protected function SaveThemeArticles($blog)
    {
    	$entry = Mozg_Mapper::Get('Articles')->GetOne(array('id' => $this->GetMainThemeId($blog->GetId()), 'blogId' => $blog->GetId()));
		if ($entry)
		{
			$themeEntryId = (int)Mozg::Request('http')->getPost('theme_id');
			$relatedEntries = $entry->GetRelatedEntries();

			$themeEntry = Mozg_Mapper::Get('Articles')->GetOne(array('id' => $themeEntryId, 'blogId' => $blog->GetId()));

			if ($themeEntry && !in_array($themeEntryId, $relatedEntries) && ($themeEntryId != $entry->GetId()))
			{
				$relatedEntries[] = $themeEntryId;
				$entry->SetRelatedEntries($relatedEntries);

				if (Mozg_Mapper::Get('Articles')->Save($entry))
				{
					$data = $this->GetTpl('ThemeList')->GetSingleRow($this->GetName(), $blog, $entry, $themeEntry);
					Mozg::Response('http')->SetBody($data);
				}
			}
		}
    }

    protected function DelThemeArticle($blog)
    {
    	$entry = Mozg_Mapper::Get('Articles')->GetOne(array('id' => $this->GetMainThemeId($blog->GetId()), 'blogId' => $blog->GetId()));
		if ($entry)
		{
			$themeEntryId = (int)Mozg::Request('http')->getQuery('theme_id');
			$relatedEntries = $entry->GetRelatedEntries();
			if (($key = array_search($themeEntryId, $relatedEntries)) !== false)
			{
				unset($relatedEntries[$key]);
				$entry->SetRelatedEntries($relatedEntries);
				if (Mozg_Mapper::Get('Articles')->Save($entry))
				{
					return;
				}
			}
		}

		Mozg::Response('http')->SetStatus(Mozg_Response::StatusError);
    }

    protected function OutComments($blog)
    {
    	$entry = $this->GetEntryFromQueryId($blog->GetId());

        $t = $this->GetTpl('Comments');
        Mozg::Response('http')->SetBody($t->GetList($this->GetName(), $blog, $entry));
    }

    protected function DelComment($blog)
    {
    	$entry = $this->GetEntryFromQueryId($blog->GetId());
		if (!$entry)
		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('ARTICLE_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg::Router('http')->Redirect('module=admin&ctrl=' .$this->GetName(). '&action=list&blog=' .$blog->GetId());
		}

		if (Mozg_Mapper::Get('Comments')->Delete(array('id' => Mozg::Request('http')->getQuery('comment'), 'entryId' => $entry->GetId())))
		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('COMMENT_DELETED'));
		}

       	Mozg::Router('http')->Redirect('module=admin&ctrl=' .$this->GetName(). '&action=commentslist&blog=' .$blog->GetId(). '&id=' .$entry->GetId());
    }
}